package com.example.administrator.dagger2demo.ui;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.dagger2demo.R;
import com.example.administrator.dagger2demo.utils.SystemBarTintManager;

/**
 * Created by Administrator on 2016/4/19.
 */
public abstract class Base2Activity extends AppCompatActivity {

    protected void initViewHeader() {
        Toolbar id_toolbar = (Toolbar) findViewById(R.id.id_toolbar);
        if (id_toolbar != null) {
            id_toolbar.setTitle("");
            setSupportActionBar(id_toolbar);

            id_toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(Base2Activity.this, "Left", Toast.LENGTH_SHORT).show();
                }
            });
            //标题栏信息
            TextView index_tv_title = (TextView) findViewById(R.id.index_tv_title);
            index_tv_title.setText(getResources().getString(showTitle()));
            //是否显示左边图标
            if (isShowLeftIcon()) {
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                id_toolbar.setNavigationIcon(setLeftIcon());
            } else {
                getSupportActionBar().setDisplayHomeAsUpEnabled(false);
            }
            //是否显示右边图标
            if (isShowRightIcon()) {
                ImageView index_iv_right = (ImageView) findViewById(R.id.index_iv_right);
                index_iv_right.setVisibility(View.VISIBLE);
                index_iv_right.setBackgroundResource(setRightIcon());
                index_iv_right.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(Base2Activity.this, "Right", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentView());
        initViewHeader();
        //判断SDK版本是否大于或等于19
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(R.color.transparent);//通知栏所需颜色
    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    /**
     * 加载布局文件
     *
     * @return
     */
    public abstract int getContentView();

    /**
     * 是否左边图标按钮
     *
     * @return
     */
    public abstract boolean isShowLeftIcon();

    /**
     * 是否右边图标按钮
     *
     * @return
     */
    public abstract boolean isShowRightIcon();

    /**
     * 更新右边图标按钮
     *
     * @return
     */
    public abstract int setLeftIcon();

    /**
     * 更新右边图标按钮
     *
     * @return
     */
    public abstract int setRightIcon();

    /**
     * 显示标题栏
     *
     * @return
     */
    public abstract int showTitle();

}
